<?php
/**
 * Created by PhpStorm.
 * User: ylq
 * Date: 2018/8/7
 * Time: 15:27
 */

namespace app\api\core;
/**
 * 获取系统
 * Class System
 * @package app\api\core
 */
class System
{
    public static function currentTimeMillis()
    {
        list ($usec, $sec) = explode(" ", microtime());

        return $sec . substr($usec, 2, 3);
    }
}

/**
 * 获取ip
 * Class NetAddress
 * @package app\api\core
 */
class NetAddress
{
    var $Name = 'localhost';
    var $IP = '127.0.0.1';
    public static function getLocalHost() //  static
    {
        $address = new NetAddress();
        $address->Name = getenv('COMPUTERNAME');
        $address->IP = $_SERVER["SERVER_ADDR"];
        return $address;
    }
    function toString() {
        return strtolower($this->Name . '/' . $this->IP);
    }
}

/**
 * 获取随机数
 * Class Random
 * @package app\api\core
 */
class Random {
    public static function nextLong()
    {
        $tmp = rand(0, 1) ? '-' : '';
        return $tmp . rand(1000, 9999) . rand(1000, 9999) . rand(1000, 9999) . rand(100, 999) . rand(100, 999);
    }
}

/**
 * 产生uuid
 * Class CreateUuid
 * @package app\api\core
 */
class CreateUuid
{
    var $valueBeforeMD5;
    var $valueAfterMD5;

    /**
     * 生产uuid
     * CreateUuid constructor.
     */
    public function __construct()
    {
        $address = NetAddress :: getLocalHost();
        $this->valueBeforeMD5 = $address->toString() . ':' . System :: currentTimeMillis() . ':' . Random :: nextLong();
        $this->valueAfterMD5 = md5($this->valueBeforeMD5);
    }

    /**
     * 转换成字符串
     * @return string
     */
    public function toString($upper = false)
    {
        $raw = $upper ? strtoupper($this->valueAfterMD5) : $this->valueAfterMD5;
        return substr($raw, 0, 8) . substr($raw, 8, 4) . substr($raw, 12, 4) . substr($raw, 16, 4) . substr($raw, 20);
    }

}
